import DataLoader from 'dataloader';

import mongo from './mongo';

// Appends type information to an object, e.g. { id: 1 } => { __type: 'User', id: 1 };
function assignType(obj, type) {
    // eslint-disable-next-line no-param-reassign, no-underscore-dangle
    obj.__type = type;
    return obj;
}

function mapTo(keys, keyFn, type, rows) {
    if (!rows) return mapTo.bind(null, keys, keyFn, type);
    const group = new Map(keys.map(key => [key, null]));
    rows.forEach(row => group.set(keyFn(row), assignType(row, type)));
    return Array.from(group.values());
}

function mapToMany(keys, keyFn, type, rows) {
    if (!rows) return mapToMany.bind(null, keys, keyFn, type);
    const group = new Map(keys.map(key => [key, []]));
    rows.forEach(row => group.get(keyFn(row)).push(assignType(row, type)));
    return Array.from(group.values());
}

function mapToValues(keys, keyFn, valueFn, rows) {
    if (!rows) return mapToValues.bind(null, keys, keyFn, valueFn);
    const group = new Map(keys.map(key => [key, null]));
    rows.forEach(row => group.set(keyFn(row), valueFn(row)));
    return Array.from(group.values());
}

export default {
    create: () => ({})
};
